import turtle

# 初始化人和鳄鱼的位置
positionHuman = (0.00, 86.00)
positionCrocodile1 = (-150.00, 0.00)
positionCrocodile2 = (150.00, 0.00)
positionCrocodile3 = (0.00, 260.00)
escapeDregree = 240

turtle.pensize(3)
time = 0  # 记录人的存活时间
for x in range(1000):  # 增加循环次数，确保能模拟足够长的时间
    turtle.color("black")
    turtle.penup()
    turtle.goto(positionHuman)
    turtle.pendown()
    # 根据贪心算法调整人的逃跑方向
    lengthCrocodile1 = turtle.distance(positionCrocodile1)
    lengthCrocodile2 = turtle.distance(positionCrocodile2)
    lengthCrocodile3 = turtle.distance(positionCrocodile3)
    min_distance = min(lengthCrocodile1, lengthCrocodile2, lengthCrocodile3)
    if min_distance == lengthCrocodile1:
        escapeDregree = escapeDregree - 20
    elif min_distance == lengthCrocodile2:
        escapeDregree = escapeDregree + 20
    turtle.setheading(escapeDregree)
    turtle.fd(10)

    positionHuman = turtle.position()
    time += 1  # 每次移动时间增加1个单位

    turtle.color("green")
    turtle.penup()
    turtle.goto(positionCrocodile1)
    turtle.pendown()
    positionCrocodile1ToHuman = turtle.towards(positionHuman)
    turtle.setheading(positionCrocodile1ToHuman)
    turtle.forward(15)
    positionCrocodile1 = turtle.position()

    turtle.color("red")
    turtle.penup()
    turtle.goto(positionCrocodile2)
    turtle.pendown()
    positionCrocodile2ToHuman = turtle.towards(positionHuman)
    turtle.setheading(positionCrocodile2ToHuman)
    turtle.forward(20)
    positionCrocodile2 = turtle.position()

    turtle.color("brown")
    turtle.penup()
    turtle.goto(positionCrocodile3)
    turtle.pendown()
    positionCrocodile3ToHuman = turtle.towards(positionHuman)
    turtle.setheading(positionCrocodile3ToHuman)
    turtle.forward(20)
    positionCrocodile3 = turtle.position()

    # 判断是否有鳄鱼追上人
    d1 = turtle.distance(positionHuman, positionCrocodile1)
    d2 = turtle.distance(positionHuman, positionCrocodile2)
    d3 = turtle.distance(positionHuman, positionCrocodile3)
    if d1 <= 10 or d2 <= 10 or d3 <= 10:
        print(f"人存活的时间: {time} 秒")
        break

turtle.done()
